package com.dal.module.account.account_creator;

import java.util.Map;

import org.error.LoginErrorCode;

import com.dal.constant.ParamKey;
import com.dal.db.bean.Account;
import com.dal.db.bean.ChannelApp;
import com.dal.db.cache.AccountCache;
import com.sojoys.artifact.tools.ToolError;
import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.crypto.SecureUtil;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * 抽象账号创建器
 * Created by fxf on 2017/6/26.
 */
@SuppressWarnings("rawtypes")
public abstract class AbstractAccountCreator implements AccountCreator{


    @Override
    public Account create(ChannelApp channelApp,Map param) {
        Account account = new Account();
        // 查看是否需要激活码
        account.setActivate(!channelApp.isNeedActivateCode());
        // 设置渠道
        account.setChannelId(channelApp.getChannelId());
        
        // 查看是否需要密码
        if (channelApp.isNeedPassword()) {
        	String password = ToolMap.getString(ParamKey.PASSWORD, param,"");
        	ToolError.isAndTrue(LoginErrorCode.PASSWORD_INVALID, StrUtil.isBlank(password));
        	// 做MD5加密
        	password = SecureUtil.md5(password);
        	account.setPassword(password);
		}else {
			account.setPassword("");
		}
        
        // 处理各渠道特殊情况
        create(channelApp,account, param);
        // 设置服务器ID
        account.setGameServerId(ToolMap.getString(ParamKey.SERVER_ID, param,""));
        // 入库
        AccountCache.me().insert(account);
//        // 填充附加信息
//        fillDeviceInfo(channelApp,account, param);
        return account;
    }

//    /**
//     * 填充设备信息
//     */
//	protected void fillDeviceInfo(ChannelApp channelApp,Account account, Map param) {
//        account.setGameServerId(ToolMap.getString(ParamKey.SERVER_ID, param,""));
//        account.setOsName(ToolMap.getString(ParamKey.OS_NAME, param,""));
//        account.setOsVersion(ToolMap.getString(ParamKey.OS_VERSION, param,""));
//        account.setDeviceId(ToolMap.getString(ParamKey.DEVICE_ID, param,""));
//        account.setDeviceToken(ToolMap.getString(ParamKey.DEVICE_TOKEN, param,""));
//        account.setDeviceName(ToolMap.getString(ParamKey.DEVICE_NAME, param,""));
//        account.setChannelId(channelApp.getChannelId());
//        account.setChannelAppId(channelApp.getId());
//        account.setSdk(ToolMap.getString(ParamKey.SDK, param,""));
//        account.setSdkVersion(ToolMap.getString(ParamKey.SDK_VERSION, param,""));
//        account.setIp(ToolMap.getString(ParamKey.CLIENT_IP, param,""));
//
//        AccountCache.me().update(account);
//    }

	/**
	 * 渠道特有处理
	 * @param channelApp
	 * @param account
	 * @param param
	 */
    protected abstract void create(ChannelApp channelApp,Account account, Map param);
}
